package view.dialog;

import javax.swing.*;
import javax.swing.border.Border;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import net.miginfocom.swing.MigLayout;

import java.util.*;


/**
 * Dialog that displays guest information.
 *	
 * @author Kenny Chong, Dan End, James Ju
 * CSE 333 : User Interface
 * Project HMSystem
 */
public class GuestInfoDialog extends JDialog 
{
	private String gName;
	private String gID;
	private String phoneNum;
	private String roomName;
	private String checkInTime;
	private String checkOutTime;
	
	private BufferedImage profileImage;

	private JLabel guestInfoLabel;
	private JLabel nameLabel;
	private JLabel idLabel;
	private JLabel phoneLabel;
	private JLabel roomLabel;
	private JLabel chkInTimeLabel;
	private JLabel chkOutTimeLabel;
	private JLabel imageLabel;
	
	private MigLayout layout;
	
	private JButton okayButton;
	private JButton callButton;
	private JPanel buttonPanel;
	
	/**
	 * Constructor.
	 * @param name
	 * @param id
	 * @param roomName
	 * @param checkInTime
	 * @param checkOutTime
	 * @param image
	 */
	public GuestInfoDialog(String name, String id, String roomName,
			String checkInTime, String checkOutTime, String phoneNumber, BufferedImage image) 
	{
		this.gName = name;
		this.phoneNum = phoneNumber;
		this.gID = id;
		this.roomName = roomName;
		this.checkInTime = checkInTime;
		this.checkOutTime = checkOutTime;
		
		this.profileImage = image;
		
		layout = new MigLayout();
		this.setLayout(layout);
		
		this.init();
	}
	
	/**
	 * Initialize dialog.
	 */
	public void init()
	{
		// Set title.
		this.setTitle("Guest Profile");
		
		// Initialize labels.
		guestInfoLabel = new JLabel("Guest Profile:");
		nameLabel = new JLabel("Guest Name: " + gName);
		idLabel = new JLabel("Guest's ID: " + gID);
		phoneLabel = new JLabel("Guest's Phone Number: " + phoneNum);
		roomLabel = new JLabel("Residing Room: " + roomName);
		chkInTimeLabel = new JLabel("Check-In Time: " + checkInTime);
		chkOutTimeLabel = new JLabel("Check-Out Time: " + checkOutTime);
		
		if (profileImage != null)
		{
			imageLabel = new JLabel(new ImageIcon(profileImage));
		
		} else {
			imageLabel = new JLabel("No Image Avaliable");
			imageLabel.setBackground(Color.white);
		}
	
		
		Border lineBorder = BorderFactory.createLineBorder(Color.black);
		imageLabel.setBorder(lineBorder);
		
		okayButton = new JButton(new ImageIcon("images/check.png"));
		callButton = new JButton(new ImageIcon("images/phone.png"));
		callButton.setToolTipText("Call Guest Via Skype");
		
		buttonPanel = new JPanel();
		
		buttonPanel.add(okayButton);
		buttonPanel.add(callButton);
		
		JPanel infoPanel = new JPanel(new MigLayout());
		infoPanel.setBorder(lineBorder);
	
		infoPanel.add(guestInfoLabel, "wrap");
		infoPanel.add(nameLabel, "wrap");
		infoPanel.add(idLabel, "wrap");
		infoPanel.add(phoneLabel, "wrap");
		infoPanel.add(roomLabel, "wrap");
		infoPanel.add(chkInTimeLabel, "wrap");
		infoPanel.add(chkOutTimeLabel, "wrap");
		infoPanel.add(okayButton, "split 2, gaptop 5");
		infoPanel.add(callButton, "wrap, gaptop 5");
		
		// Add components to dialog. 
		this.add(imageLabel, "dock west");
		this.add(infoPanel, "dock west,wrap, gapleft 10, split 2");
		
		this.pack();
		this.setModal(true);
		this.setResizable(false);
		
		okayButton.addActionListener(new OkayListener());
		callButton.addActionListener(new CallListener());
	}
	
	/**
	 * Opens the dialog.
	 */
	public void open()
	{
		this.setVisible(true);
	}
	
	private class OkayListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent e) 
		{
			// Close dialog.
			GuestInfoDialog.this.setVisible(false);
		}
		
	}
	
	private class CallListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			JOptionPane.showMessageDialog(GuestInfoDialog.this, "Calling "
					+ gName + "...", "Calling Guest", JOptionPane.CANCEL_OPTION);
		}
	}
}
